import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {
        path:"/",
        name:"User",
        component:() => import("../views/UserList.vue")
      },
      {
        path:"/member",
        name:"Member",
        component:() => import("../views/Member.vue")
      },
      {
        path:"/jurisdiction",
        name:"Jurisdiction",
        component:() => import("../views/Jurisdiction.vue")
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path:"/login",
    name:"Login",
    component:() => import("../views/Login.vue")
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//全局路由守卫
/*
to:将要切换的路由对象
from:即将离开的路有对象
next:下一步函数
*/

router.beforeEach((to,from,next)=>{
  var userLogonStatus = localStorage.getItem("token");
  if(to.name !== "Login"){
    if(userLogonStatus){
      next()
    }else{
      next("/login")
    }
  }else{
    next();
  }
  
})

export default router
